// 操作人ID
var tokenJson = JSON.parse(decodeURIComponent(web.readCookie("loginData")));

var approvalProduct = {
    timer:null,//服务商模糊查询防抖操作
    params: {
        providerId: '',//服务商ID
        spuName: "",//商品名称
        specifications: "",//规格
        customerCode:"",// 专属客户
        auditStatus: "",//审核状态  10:待审核 20:审核通过 30:审核驳回
        pageNum: 1,
        pageSize: 20,
    },
    //初始化数据
    init: function () {
        this.addClick()
        this.getDataInfo();
        this.getCooperationByProviderInfo();// 获取所有渠道
    },
    // 事件处理
    addClick: function () {
        // 查询
        $('#searchBtn').on('click', function () {
            approvalProduct.params.spuName = $('#spuName').val();
            approvalProduct.params.specifications = $('#specifications').val();
            approvalProduct.params.auditStatus = $("#status").find("option:selected").val();
            approvalProduct.params.channelId  = $("#belongChannel").find("option:selected").val();
            approvalProduct.params.pageNum = 1;
            approvalProduct.params.pageSize = 20;
            approvalProduct.getDataInfo();
        });
        // 重置
        $('#resetBtn').on('click', function () {
            approvalProduct.params.providerId = '';
            approvalProduct.params.spuName = '';
            approvalProduct.params.specifications = '';
            approvalProduct.params.auditStatus = '';
            approvalProduct.params.pageNum = 1;
            approvalProduct.params.pageSize = 20;
        });
        // 服务商模糊搜索
        $('#providerId').on('keyup', function () {
            clearTimeout(approvalProduct.timer);
            approvalProduct.timer = setTimeout(function(){
                approvalProduct.findAllInfo($('#providerId').val());
                $('.search').show();
            },1000)

        });
        // 选择服务商查询结果
        $('.search').delegate('.search-item', 'click', function () {
            var itemId = $(this).attr('data-id');
            var itemName = $(this).text();
            $('.service-productName').val(itemName);
            $('.search').hide();
            approvalProduct.params.providerId = itemId;
        });
        $(document).on('click',function(){
            $('.search').hide();
        })
        // 专属渠道
        $('#belongChannel').on('click', function () {
         /*   if (!$('#providerId').val()) {
                layer.msg('请先选择服务商',{offset:'t'})
            }*/
        });

    },
    // 获取列表数据
    getDataInfo: function () {
        var successData = function (data) {
            if (data.errorCode == 0) {
                if (!!data.data && data.data.data.length > 0) {
                    approvalProduct.renderListsData(data.data.data)
                    $(".M-box3").pagination({
                        pageCount: data.data.pageCount,
                        jump: true,
                        coping: true,
                        prevContent: '上页',
                        nextContent: '下页',
                        callback: function (api) {
                            approvalProduct.params.pageNum = api.getCurrent()
                            var successData = function (data) {
                                approvalProduct.renderListsData(data.data.data)
                            }
                            return web.doAjax(root.serviceProviderManagement.auditListPageByAdmin, 'post', approvalProduct.params, successData, 1)
                        }
                    })
                } else {
                    $(".M-box3").pagination({
                        pageCount: 0
                    })
                    approvalProduct.renderListsData(data.data.data)
                }
            } else {
                layer.msg('数据异常')
            }

        }
        return web.doAjax(root.serviceProviderManagement.auditListPageByAdmin, 'post', approvalProduct.params, successData, 1);
    },
    // 渲染列表数据
    renderListsData: function (data) {
        var ary = [];
        var obj = {};
        if (data.length > 0) {
            data.forEach(function (item) {
                if (!obj[item.spuAuditRecordId]) {
                    ary.push({
                        spuAuditRecordId: item.spuAuditRecordId,
                        providerName: item.providerName,
                        spuCode:item.spuCode,
                        result: [item]
                    });
                    obj[item.spuAuditRecordId] = item
                } else {
                    ary.forEach(function (item2) {
                        if (item2.spuAuditRecordId == item.spuAuditRecordId) {
                            item2.result.push(item)
                        }
                    })
                }
            })
            approvalProduct.tableLists(ary)
        } else {
            var emptyStr = '<div style="background: #ffff;text-align: center;font-size: 14px;padding-top: 10px;">暂无商品</div>';
            $('#table-lists').html(emptyStr)
        }

    },
    // 表格数据拼接
    tableLists: function (data) {
        var tableLists = $('#table-lists');
        var tableDataStr = '';
        data.forEach(function (item) {
            var providerName = item.providerName?item.providerName:'';//服务商名称
            var spuCode = item.spuCode?item.spuCode:'';//spu码
            var tableData = item.result;// 表格里面的数据
            var multipleExamine = ''
            var titleStr = '';
            var newStr = '';
            var itemTable = '';
            var multifyCheckSkuIds = [];
            // 获取审核信息id
            if (tableData.length > 1) {
                tableData.forEach(function (item) {

                    // 待审核
                    if (item.auditStatus == 10) {
                        multifyCheckSkuIds.push(item.skuAuditRecordId)
                    }
                })
                if (multifyCheckSkuIds.length > 1) {
                    multipleExamine = '批量审核';
                }
            } else {
                multipleExamine = ''
            }
            titleStr = ' <div class="title">' +
                '                   <div class="title-left">' +
                '                       <span>服务商名称：</span>' +
                '                       <span>' + providerName + '</span>' +
                '                       <span class="spuCode">SPU码：' + spuCode + '</span>' +
                '                   </div>' +
                '                    <div class="title-right" onclick="approvalProduct.multifyCheck( ' + JSON.stringify(multifyCheckSkuIds) + ')">' +
                multipleExamine +
                '                    </div>' +
                '                </div>'
            tableData.forEach(function (item) {
                var status = '';
                var operateStr = '';// 操作的按钮的字符串
                if (item.auditStatus == 10) {
                    status = '待审核'
                } else if (item.auditStatus == 20) {
                    status = '审核通过'
                } else if (item.auditStatus == 30) {
                    status = '审核驳回'
                } else {
                    status = '';
                }
                if (item.auditStatus == 10) {
                    operateStr = '<td width="80" class="operate-btn" onclick="approvalProduct.checkExamine(' + item.skuAuditRecordId + ')">审核</td>'
                } else {
                    operateStr = '<td width="80" class="operate-btn" onclick="approvalProduct.queryDetail(' + item.skuAuditRecordId + ')">查看详情</td>'
                }
                var channelPrice = item.channelPrice?item.channelPrice.toFixed(2):0.00;
                var xfsPurchasePrice = item.xfsPurchasePrice?item.xfsPurchasePrice.toFixed(2):0.00;
                var unitName = item.unitName ? '/' + item.unitName : '';
                itemTable +=
                    '<tr>' +
                    '           <td width="190" class="text-l" style="padding-left: 20px">' +
                    '              <img src="' + item.spuImages[0].attachmentUrl + '" alt="" class="table-img">' +
                    '                   <div class="img-title">' +
 /*                   '                         <p>' + item.spuName + '</p>' +
                    '                          <p>' + item.specifications + '</p>' +*/
                    '                        <p>' + item.skuName + '</p> '+
                    '                    </div>' +
                    '              </td>' +
                    '              <td width="80">' + item.skuCode + '</td>' +
                    '              <td width="100">' + item.channelName  + '</td>' +
                    '              <td width="80">￥' + channelPrice + unitName +'</td>' +
                    '              <td width="80">￥' + xfsPurchasePrice + unitName +'</td>' +
                    '              <td width="180">' + item.updateTime + '</td>' +
                    '              <td width="100">' + status + '</td>' + operateStr +
                    '</tr>';
            });
            newStr += titleStr + '<table class="table table-border table-bg table-hover table-sort">' + itemTable + '</table>';
            tableDataStr += newStr;
        })
        tableLists.html(tableDataStr)
    },
    // 获取服务商数据
    findAllInfo: function (name) {
        var params = {
            status: '',
            name: name
        }
        var successData = function (data) {
            if (data.code == 0) {
                if (data.data && data.data.length > 0) {
                    var result = data.data;
                    var str = ''
                    result.forEach(function (item) {
                        str += '<li class="search-item" data-id="' + item.id + '">' + item.name + '</li>'
                    });
                    $('.search').html(str)
                } else {
                    var emptyStr = '<li class="search-item">暂无数据</li>';
                    $('.search').html(emptyStr)
                }
            }
        }
        return web.doAjax(root.serviceProviderManagement.findAll, 'post', params, successData, 2);
    },
    // 获取专属渠道
    getCooperationByProviderInfo: function () {
        var params = {
            providerId: ''
        }
        var successData = function (data) {
            if (data.code == 0) {
                if (data.data.length > 0) {
                    var result = data.data;
                    var str = '';
                    result.forEach(function (item) {
                        str += '<option value="' + item.channelId + '">' + item.channelName + '</option>'
                    });
                    var newStr = ' <option value="">请选择专属渠道</option>' + str;
                    $('#belongChannel').html(newStr)
                } else {
                    var emptyStr = '<option value="">暂无结果</option>';
                    $('#belongChannel').html(emptyStr)
                }
            }else {
                layer.msg(data.msg);
            }
        }
        return web.doAjax(root.serviceProviderManagement.findProviderCooperationByProviderId, 'post', params, successData, 2);
    },

    // 审核
    checkExamine: function (skuAuditRecordId) {
        var url = layer.open({
            type: 2,
            title: '审核',
            content: 'pendingExamination.html?skuId=' + skuAuditRecordId
        });
        layer.full(url);
    },
    // 查看详情
    queryDetail: function (skuId) {
        var url = layer.open({
            type: 2,
            title: '审核',
            content: 'pendingQueryDetail.html?params=' + skuId
        });
        layer.full(url);
    },
    // 批量审核
    multifyCheck: function (skuIds) {
        sessionStorage.setItem('skuIds', skuIds)
        var url = layer.open({
            type: 2,
            title: '审核',
            content: 'pendingExamination.html'
        });
        layer.full(url);
    }


}
approvalProduct.init();


//待审核商品导出
function auditProductExport()
{
    var providerId = approvalProduct.params.providerId;
    var spuName = $("#spuName").val();
    var specifications = $("#specifications").val();
    var auditStatus = $("#status").val();
    var channelId = $("#belongChannel").find("option:selected").val();
    var exportUrl = root.serviceProviderManagement.auditProductExport + "?number=" + providerId + "&spuName=" + spuName + "&specifications=" + specifications + "&auditStatus=" + auditStatus + "&channelId=" + channelId;
    window.location.href = exportUrl;
}